package com.ms.qw.jsf.converter;

import com.ms.qw.application.mappers.ComunMapper;
import com.ms.qw.client.dto.CodigoValorDTO;
import com.ms.qw.enums.TipoCodigoValor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import java.util.HashMap;
import java.util.Map;

/**
 * User: aglwkrs
 * Date: 30/11/11
 */
@Component
@FacesConverter("com.ms.qw.jsf.converter.ManagerTipoRecetaConverter")
public class ManagerTipoRecetaConverter implements Converter {
    @Autowired
    ComunMapper comunMapper;

    public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
        if (StringUtils.isBlank(value) || value.equals("Seleccione") || value.equals("Todos")) {
            return null;
        }
        Map map = new HashMap();
        map.put("codigo", TipoCodigoValor.TIPO_RECETA.getCodigo());
        map.put("valor", value);
        return comunMapper.obtenerValor(map);
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
        if (value == null || !(value instanceof CodigoValorDTO)) {
            return null;
        }
        return String.valueOf(((CodigoValorDTO) value).getCodigo());
    }


}
